<?php

namespace Common\Model;

/**
 * 用户开门权限记录表
 * @modify trainyao 2016-01-07 创建
 * @index userlogin_id (`isdelete`, `userlogin_id`)
 */
Class UserRoomModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_user_room';

    protected function _before_insert(&$data, $options) {
        
    }

    protected function _before_update(&$data, $options) {
        
    }

    /**
     * 根据ID获取数据
     * @modify trainyao 2016-08-26 创建
     * @param  mixin   $ids       条目ID(s)
     * @return array              数据数组, [{id, printcount, startdate, enddate}, {}, {},...]
     */
    public function getInfo($ids) {
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }

        $option = array(
            'id' => count($ids) == 1 ? $ids[0] : array('in', $ids),
        );
        $data = $this->where($option)->select();
        return !empty($data) ? $data : array();
    }

    /**
     * 订单房间ID返回所有用户房间
     * @modify songanwei 2016-01-07
     * @modify trainyao 2016-01-14 修改为可查多个子订单入住人
     * @param mixin $orderRoomId    子订单id(s)
     * @return type
     */
    public function getInfosByOrderRoom($orderRoomId) {
        $where = array();
        if (is_array($orderRoomId)) {
            $orderRoomId = implode(',', $orderRoomId);
        }
        if (strpos($orderRoomId, ',') !== false) {
            $where['orderroom_id'] = array('in', $orderRoomId);
        } else {
            $where['orderroom_id'] = $orderRoomId;
        }
        return $this->where($where)->select();
    }

    /**
     * 订单用户ID返回用户所有房间数据
     * @modify songanwei 2016-01-11
     * @modify trainyao  2016-01-19       修改为可以选择多个OrderUser的数据
     * @param mixin $orderUserId 订单用户ID(s)
     * @return type
     */
    public function getInfosByOrderUser($orderUserId) {
        $where = array();
        if (is_array($orderUserId)) {
            $orderUserId = implode(',', $orderUserId);
        }
        if (strpos($orderUserId, ',') !== false) {
            $where['orderuser_id'] = array('in', $orderUserId);
        } else {
            $where['orderuser_id'] = $orderUserId;
        }
        
        return $this->where($where)->select();
    }

    /**
     * 根据预订者id获取房卡
     * @modify trainyao 2016-05-11 创建
     * @param  integer   $userId    预定人ID
     * @return array              信息数组
     */
    public function getInfoByUser($userId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'userlogin_id' => $userId,
        );
        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 
     * 添加用户房间
     * @modify songanwei 2016-01-06
     * @modify trainyao 2016-04-20 修改为增加多条
     * @param array $infoArray 信息数组
     * @return minin 成功返回id,失败返回false
     */
    public function addInfo($infoArray) {
        $result = $this->addAll($infoArray);
        return empty($result) ? false : $result;
    }

    /**
     * 修改用户房间数据
     * @modify songanwei 2016-01-07
     * @modify trainyao 2016-01-15 修改成修改部分数据
     * @param integer $userRoomId  用户房间ID
     * @param integer $userLoginId 用户ID
     * @param integer $orderRoomId 房间订单ID
     * @param integer $roomId 入住开始日期
     * @param date $startDate 入住结束日期
     * @param date $endDate
     */
    public function saveInfo($userRoomId, $userLoginId = null, $orderRoomId, $roomId, $startDate, $endDate) {
        $options = array();
        if (!is_null($userLoginId)) {
            $options['userlogin_id'] = $userLoginId;
        }
        if (!empty($orderRoomId)) {
            $options['orderroom_id'] = $orderRoomId;
        }
        if (!empty($roomId)) {
            $options['roominfo_id'] = $roomId;
        }
        if (!empty($startDate)) {
            $options['startdate'] = $startDate;
        }
        if (!empty($endDate)) {
            $options['enddate'] = $endDate;
        }
        $result = $this->where(array('id' => $userRoomId))->save($options);
        return $result === false ? false : true;
    }

    /**
     * 用户房间更新数据
     * @modify songanwei 2016-01-11
     * @param integer $orderUserId 订单用户ID
     * @param integer $orderRoomId 订单房间ID
     * @param integer $userloginId 用户登录ID
     * @param integer $roomId 房间ID
     * @param date $startDate 开始日期
     * @param date $endDate 结束日期
     * @return type
     */
    public function saveInfoUserRoom($orderUserId, $orderRoomId, $userloginId, $roomId, $startDate, $endDate) {
        $options = array(
            'userlogin_id' => $userloginId,
            'roominfo_id' => $roomId,
            'startdate' => $startDate,
            'enddate' => $endDate
        );
        $where = array(
            'orderuser_id' => $orderUserId,
            'orderroom_id' => $orderRoomId,
            'isdelete' => NOT_DELETE
        );
        $result = $this->where($where)->save($options);
        return $result === false ? false : true;
    }

    /**
     * 保存用户已打印次数
     * @modify trainyao 2016-08-23  创建
     * @param  mixin   $ids        子订单ID(s)
     * @param  integer   $printCount 打印次数
     * @return boolean               执行结果
     */
    public function savePrintCount($ids, $printCount) {
       if (!is_array($ids)) {
           $ids = explode(',', $ids);
       }
       $option = array(
           'id' => count($ids) == 1 ? $ids[0] : array('in', $ids),
       );
       $data = array(
           'printcount' => $printCount,
       );
       return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 保存用户已打印次数
     * @modify trainyao 2016-08-23  创建
     * @param  mixin   $ids        子订单ID(s)
     * @param  integer   $printCount 打印次数
     * @return boolean               执行结果
     */
    public function saveRfCount($ids, $printCount) {
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        $option = array(
            'id' => count($ids) == 1 ? $ids[0] : array('in', $ids),
        );
        $data = array(
            'rfcount' => $printCount,
        );
        return $this->where($option)->save($data) === false ? false : true;
    }
    
    /**
     * 删除订单下的房间用户数据
     * @modify songanwei 2016-01-07
     * @param integer $orderRoomId  订单房间ID
     * @param mixed $roomIds 房间ID
     * @return type
     */
    public function deleteInfoByOrderRooms($orderRoomId, $roomIds) {
        $where['isdelete'] = NOT_DELETE;
        $where['orderroom_id'] = $orderRoomId;
        if (is_array($roomIds)) {
            $roomIds = implode(',', $roomIds);
        }
        if (strpos($id, ',') !== false) {
            $where['roominfo_id'] = array('in', $roomIds);
        } else {
            $where['roominfo_id'] = $roomIds;
        }
        $result = $this->where($where)->save(array('isdelete' => IS_DELETE));
        return $result === false ? false : true;
    }

    /**
     * 根据子订单删除房间权限
     * @modify trainyao 2016-01-07   创建
     * @param  mixin   $orderRoomId 子订单id(s)
     * @return boolean                执行结果
     */
    public function deleteInfoByOrderRoom($orderRoomId) {
        $option = array(
            'isdelete' => NOT_DELETE
        );
        if (is_array($orderRoomId)) {
            $orderRoomId = implode(',', $orderRoomId);
        }
        if (strpos($orderRoomId, ',') !== false) {
            $option['orderroom_id'] = array('in', $orderRoomId);
        } else {
            $option['orderroom_id'] = $orderRoomId;
        }
        $data = array(
            'isdelete' => IS_DELETE
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据订单用户删除用户房间数据
     * @modify songanwei 2016-01-11
     * @param integer $orderUserId  订单用户ID
     * @param integer $orderRoomId  订单房间ID
     * @return type
     */
    public function deleteInfoByOrderUser($orderUserId, $orderRoomId = 0) {
        $where['isdelete'] = NOT_DELETE;
        $where['orderuser_id'] = $orderUserId;
        if ($orderRoomId > 0) {
            $where['orderroom_id'] = $orderRoomId;
        }
        $result = $this->where($where)->save(array('isdelete' => IS_DELETE));
        return $result === false ? false : true;
    }

    /**
     * 根据ID删除数据
     * @modify trainyao 2016-04-20 创建
     * @param mixin $ids
     * @return boolean 删除成功与否
     */
    public function deleteInfo($id) {
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') !== false) {
            $option['id'] = array('in', $id);
        } else {
            $option['id'] = $id;
        }
        $data = array(
            'isdelete' => IS_DELETE
        );

        return $this->where($option)->save($data) === false ? false : true;
    }
}
